<template>
  <div>
    <h1>评论列表</h1>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>评论管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @change="getCommentList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getCommentList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="commentList" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="content" prop="content"></el-table-column>
        <el-table-column label="date" prop="date"></el-table-column>
        <el-table-column label="user" prop="user.username"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip
              effect="dark"
              content="编辑用户"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                @click="openUpdateComment(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              effect="dark"
              content="删除用户"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                v-on:click="openDeleteComment(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNumber"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 添加用户对话框 -->
    <!-- 修改用户对话框 -->
    <el-dialog
      title="修改评论"
      :visible.sync="updateCommentDialogVisible"
      width="50%"
      @close="updateDialogClosed"
    >
      <!-- 内容主题区域    :rules="addFormRules"      :model="updateUserData" -->
      <el-form ref="updateCommentFormRef" label-width="70px">
        <el-form-item label="id" prop="id">
          <el-input v-model="updateCommentData.id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="updateCommentData.content"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateCommentDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateComment">修 改</el-button>
      </span>
    </el-dialog>

    <!-- 删除用户对话框 -->
    <el-dialog
      title="删除歌曲"
      :visible.sync="deleteCommentDialogVisible"
      width="30%"
    >
      <!-- 内容主题区域 -->
      <h2>你确定要删除评论</h2>
      <el-form
        :model="deleteCommentData"
        ref="updateCommentFormRef"
        label-width="70px"
      >
        <el-form-item label="id" prop="id">
          <el-input v-model="deleteCommentData.id"></el-input>
        </el-form-item>
      </el-form>

      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteCommentDialogVisible = false">取 消</el-button>
        <el-button type="danger" @click="deleteComment">删 除</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pageNumber: 1,
        pageSize: 5
      },
      commentList: [],
      total: 0,
      dialogVisible: false,
      updateCommentDialogVisible: false,
      deleteCommentDialogVisible: false,
      deleteCommentData: [],
      updateCommentData: []
    };
  },
  created() {
    this.getCommentList();
  },
  methods: {
    getCommentList() {
      this.$http({
        method: "get",
        params: this.queryInfo.query,
        url:
          "comment/" + this.queryInfo.pageSize + "/" + this.queryInfo.pageNumber
      })
        .then(response => {
          console.log(response.data);
          this.$message.info("获取用户列表成功");
          this.commentList = response.data.data.items;
          this.total = response.data.data.total;
        })
        .catch(error => {
          console.log(error);
        });
    },
    handleSizeChange(newSize) {
      console.log(newSize);
      this.queryInfo.pageSize = newSize;
      this.getCommentList();
    },
    handleCurrentChange(newPage) {
      console.log(newPage);
      this.queryInfo.pageNumber = newPage;
      this.getCommentList();
    },
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    updateDialogClosed() {
      this.$refs.updateCommentFormRef.resetFields();
    },
    openDeleteComment(rowInfo) {
      console.log(rowInfo.id);
      this.deleteCommentData = rowInfo;
      this.deleteCommentDialogVisible = true;
    },
    openUpdateComment(rowInfo) {
      console.log(rowInfo);
      this.updateCommentData = rowInfo;
      this.updateCommentDialogVisible = true;
    },
    updateComment() {
      this.updateCommentDialogVisible = false;
      console.log(this.updateCommentData);
      this.$http({
        method: "put",
        url: "comment/",
        data: this.updateCommentData
      });
    },
    deleteComment() {
      this.deleteCommentDialogVisible = false;
      this.$http
        .delete("comment/" + this.deleteCommentData.id)
        .then(response => {
          console.log(response);
          this.$message.success("删除成功");
        })
        .catch(error => {
          console.log(error);
        });
    }
  }
};
</script>

<style scoped></style>
